package cn.jiyun.demo_users.controller;

import cn.jiyun.demo_users.pojo.Users;
import cn.jiyun.demo_users.service.UsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@ResponseBody
@RequestMapping("/users")
public class UsersController {
    @Autowired
    private UsersService usersService;
    @RequestMapping("/{page}")
    public String showPage(@PathVariable String page){
        return page;
    }
    @RequestMapping("/addUsers")
    public String addUsers(Users users){
        this.usersService.addUsers(users);
        return "ok";
    }
    @RequestMapping("/listUsers")
public String listUsers(Model model){
        List<Users>list=this.usersService.listUsers();
        model.addAttribute("list",list);
        return "list";
    }
    @RequestMapping("/login")
    public String login(Model model, Users users, HttpSession session){
        Users users1=usersService.login(users);
        if (users!=null){
            session.setAttribute("user",users);
            return "redirect:/users/listUsers";
        }

        return "login";

    }
}
